package com.graduation.alien_yuan.smartmusicplayer;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

import com.graduation.alien_yuan.smartmusicplayer.utils.LogUtils;

/**
 * Created by Alien_yuan on 2016/3/14.
 */
public class AudioTrackManager {
    public static final int RATE=44100;
    public static final int LEFT=1;
    public static final int RIGHT=2;
    public static final int DOUBLE=3;

    AudioTrack audioTrack;
    float volume;
    int channel;
    int length;
    int waveLen;
    int Hz;
    byte[] wave;

    public AudioTrackManager(){
        wave=new byte[RATE];
        channel = DOUBLE;
    }

    /**
     * @param rate
     */
    public void start(int rate){
        stop();
        if(rate>0){
            Hz=rate;
            waveLen = RATE / Hz;
            length = waveLen * Hz;

            audioTrack=new AudioTrack(AudioManager.STREAM_MUSIC, RATE,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_8BIT, length, AudioTrack.MODE_STREAM);
            wave=SinWave.sin(Hz,wave, waveLen, length);

            if(audioTrack!=null){
                audioTrack.play();
            }
        }else{
            return;
        }

    }

    /**
     */
    public void play(){
        if(audioTrack!=null){
            audioTrack.setVolume(volume);
            audioTrack.write(wave, 0, length);
        }
    }

    public byte[] getWave(){
        return wave;
    }

    /**
     */
    public void stop(){
        if(audioTrack!=null){
            audioTrack.stop();
            audioTrack.release();
            audioTrack=null;
        }
    }

    private static int volLevel = 0;
    public void addVolum(){
        if (volLevel < 10){
            volLevel = volLevel + 1;
        }else {
            volLevel = 10;
        }
        this.setVolume(volLevel /10f);
    }

    public void cutVolum(){
        if (volLevel > 0){
            volLevel = volLevel - 1;
        }else {
            volLevel = 0;
        }
        this.setVolume(volLevel /10f);
    }


    public float getVolume(){
        return volume;
    }

    /**
     * @param vol
     */
    public void setVolume(float vol){
        LogUtils.d("alien-set volume:"+vol* AudioTrack.getMaxVolume());
        LogUtils.d("alien-set getMaxVolume:"+AudioTrack.getMaxVolume());
        this.volume=vol* AudioTrack.getMaxVolume();
        if(audioTrack!=null){
            switch (channel) {
                case LEFT:
                    audioTrack.setStereoVolume(volume, 0f);
                    break;
                case RIGHT:
                    audioTrack.setStereoVolume(0f, volume);
                    break;
                case DOUBLE:
                    audioTrack.setStereoVolume(volume, volume);
                    break;
            }
        }
    }

    /**
     * @param channel
     */
    public void setChannel(int channel){
        this.channel=channel;
        setVolume(volume);
    }
}
